package cn.est.controller;

import cn.est.constants.Constants;
import cn.est.dto.Result;
import cn.est.exceptions.PayException;
import cn.est.pojo.MaintainOrder;
import cn.est.service.AlipayService;
import cn.est.service.MaintainOrderService;
import cn.est.utils.ResultUtils;
import com.alipay.api.AlipayApiException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@Api
@RestController
@RequestMapping("/alipay")
public class AlipayController {



    @Autowired
    MaintainOrderService maintainOrderService;

    @Autowired
    AlipayService alipayService;

    //1.发起支付请求接口

    /**
     *
     * @param orderNo
     * @param response
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "发起支付请求",notes = "支付宝支付，发起支付请求")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "token",value = "用户token",required = true),
            @ApiImplicitParam(name = "orderNo",value = "订单号",required = true)
    })
    @GetMapping("/pay")
    public Result alipay(String orderNo,HttpServletResponse response) throws IOException {
        //1.根据oderNo,获取订单信息
        Map<String,Object> map= new HashMap<String,Object>();
        map.put("orderNo",orderNo);
        MaintainOrder maintainOrder=maintainOrderService.getMaintainOrderListByMap(map).get(0);
        if(maintainOrder==null){
            return ResultUtils.returnFail("订单号有误,请重新下单");
        }
        if(!maintainOrder.getStatus().equals(Constants.Order.AppointStatus.APPOINT)){
            return ResultUtils.returnFail("订单已支付或者没有此订单,订单不可以支付");
        }

        //2.封装请求，发送支付宝请求
        response.setContentType("text/html; charset=utf-8");
        PrintWriter out=response.getWriter();
        try {
            String  result= alipayService.pay(maintainOrder);
            out.println(result);
            out.close();
        } catch (AlipayApiException e) {
            e.printStackTrace();
            return ResultUtils.returnFail();
        }
        return ResultUtils.returnFail();

    }





    //2.同步请求接口，接受支付宝平台回调
    @GetMapping("/return_url")
    public void  returnUtil(HttpServletRequest request, HttpServletResponse response) throws IOException {

        int sucess = 0;
        try {
            sucess = alipayService.asynNotify(request);
        } catch (PayException e) {
            e.printStackTrace();
        }
        //成功返回 success  失败  failure
        if(sucess>0){
            response.getWriter().println("success");
            return;
        }
        response.getWriter().println("failture");
        return ;
    }
    //3.异步请求接口，接受支付宝平台回调

    /**
     * 公网访问
     *
     * @param request
     * @param response
     */
   @PostMapping("/notify_url")
        public void  returnUtil2(HttpServletRequest request, HttpServletResponse response) throws IOException {
            System.out.println("notify_url");
            // 发送一步处理请求
       int sucess = 0;
       try {
           sucess = alipayService.asynNotify(request);
       } catch (PayException e) {
           e.printStackTrace();
       }
       //成功返回 success  失败  failure
       if(sucess>0){
           response.getWriter().println("success");
       }
         response.getWriter().println("failture");
    }

}
